package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.hema.auction.R;
import com.hema.auction.adapter.FragAdapter;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.event.PayResultEvent;
import com.hema.auction.fragment.MyAuctionFragment;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.view.TitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的竞拍
 * author by john
 */

public class MyAuctionActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private String[] title = new String[]{"全部", "我在拍", "我拍中", "未拍中", "待付款","待发货", "待签收", "待晒单"};
    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_difference_order);
        ButterKnife.bind(this);
        titleView.setTitle(R.string.my_auction);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        fragments.add(MyAuctionFragment.newInstance(100));//全部
        fragments.add(MyAuctionFragment.newInstance(101));//我在拍
        fragments.add(MyAuctionFragment.newInstance(102));//我拍中
        fragments.add(MyAuctionFragment.newInstance(103));//未拍中
        fragments.add(MyAuctionFragment.newInstance(0));//待付款
        fragments.add(MyAuctionFragment.newInstance(1));//待发货
        fragments.add(MyAuctionFragment.newInstance(2));//待签收
        fragments.add(MyAuctionFragment.newInstance(3));//待晒单
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(), fragments, title);
        viewPager.setAdapter(fragAdapter);
        tablayout.setupWithViewPager(viewPager);
        tablayout.getTabAt(getIntent().getIntExtra(Constant.EXTRA_TAB_INDEX, 0)).select();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayResultvent(PayResultEvent event) {
        if (event.getType() == 2) {
            viewPager.setCurrentItem(5);
            MyAuctionFragment auctionFragment = (MyAuctionFragment) fragments.get(5);
            auctionFragment.setRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
